
<template>
  <div class="user-profile">
    <!-- 导航栏 -->

    <van-nav-bar
    class="page-nav-bar"
    title="个人信息"
    left-arrow @click-left="$router.back()" />
    <!-- /导航栏 -->
    <input @change="onChange" type="file"  hidden ref="inputFile">
    <!-- 点击上传头像文件 -->
    <van-cell @click="$refs.inputFile.click()" title="头像" is-link>
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="userList.photo"
      />
    </van-cell>
    <van-cell @click="showEdit=true" title="昵称" :value="userList.name" is-link />
    <van-cell @click="showEditSex=true" title="性别" :value="userList.gender?'女':'男'" is-link />
    <van-cell @click="showEditBirthday=true" title="生日" :value="userList.birthday" is-link />
<!-- 修改昵称弹出层 -->
    <van-popup v-model="showEdit" position="bottom" :style="{ height: '100%' }" >
      <user-edit v-if="showEdit" @close="showEdit=false" v-model="userList.name" />
    </van-popup>
<!-- 修改性别弹出层 -->
    <van-popup v-model="showEditSex" position="bottom" :style="{ height: '40%' }" >
      <user-edit-sex v-if="showEditSex" @close="showEditSex=false" v-model="userList.gender"/>
    </van-popup>
    <!-- 修改年龄弹出层 -->
    <van-popup v-model="showEditBirthday" position="bottom" :style="{ height: '40%' }" >
      <!-- <user-edit-sex/> -->
      <user-edit-birthday  v-if="showEditBirthday" @close="showEditBirthday=false" v-model="userList.birthday" />
    </van-popup>

        <!-- 修改头像弹出层 -->
    <van-popup v-model="showEditAvatar" position="bottom" :style="{ height: '100%' }" >
      <!-- <user-edit-sex/> -->
     <user-avatar :img="imgUrl" v-if="showEditAvatar" @update-photo="userList.photo=$event" @close="showEditAvatar=false" />
    </van-popup>
  </div>
</template>

<script>
import { loadUserInfo } from '@/api/user.js'
import UserEdit from './components/UserEdit'
import UserEditSex from './components/UserEditSex'
import UserEditBirthday from './components/UserEditBirthday'
import UserAvatar from './components/UserAvatar.vue'
export default {
  components: { UserEdit, UserEditSex, UserEditBirthday, UserAvatar },
  data () {
    return {
      userList: {},
      showEdit: false,
      showEditSex: false,
      showEditBirthday: false,
      showEditAvatar: false,
      imgUrl: ''
    }
  },

  created () {
    this.loadUserInfo()
  },

  methods: {
    async loadUserInfo () {
      try {
        const res = await loadUserInfo()
        this.userList = res
      } catch (err) {

      }
    },
    // 选择文件的输入框发生变化
    onChange (e) {
      console.log(e)
      // change事件发生改变
      if (e.target.value === '') return
      this.showEditAvatar = true
      // 如果不进行裁剪，e.target.files[0] 可以直接交给后端  e.target.files[0] 上传的文件是一个file对象
      //  URL.createObjectURL  是web api 的一个静态方法，，将其变为blob对象
      this.imgUrl = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    }

  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
